package  ui.popup
{
	import flash.display.Bitmap;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import events.GCEvent;
	import ui.GCNumberScroller;
	import ui.button.GCButton;
	import ui.textfield.*;
	/**
	 * ...
	 * @author Jason Won
	 */
	public class GCBuyPopup extends Sprite
	{
		private static const RESOURCE_LABEL:String = "Name of Good:";
		private static const QUANTITY_LABEL:String = "Quantity to Buy:";
		private static const PRICE_PER_LABEL:String = "Price per unit:";
		private static const TOTAL_PRICE_LABEL:String = "Total Sale Price:";
		
		private static const X_OFFSET:int = 20;
		private static const Y_OFFSET:int = 20;
		private static const LABEL_HEIGHT:int = 50;
		private static const LABEL_WIDTH:int = 100;
		
		/*
		Variables used to keep track of item information. Should be reset
		everytime this popup reappears
		*/
		private var _resourceName:String;
		private var _pricePerUnit:int;
		private var _quantityBought:int;
		private var _totalPrice:int;
		private var _amountForSale:uint;
		
		/*
		Clickable components in the menu. 
		*/
		private var _quantityComponent:GCNumberScroller;
		private var _confirmButton:GCButton;
		private var _cancelButton:GCButton;
		private var _buyAllButton:GCButton;
		
		/*
		Various text labels
		*/
		private var _resourceLabel:GCTextField;
		private var _quantityLabel:GCTextField;
		private var _pricePerLabel:GCTextField;
		private var _totalPriceLabel:GCTextField;
		
		/*
		Labels to display values
		*/
		private var _resourceText:GCTextField;
		private var _pricePerText:GCTextField;
		private var _totalPriceText:GCTextField;
		private var _amountForSaleText:GCTextField;
		
		/*
		 * Background 
		 */
		private var _backgroundImage:Sprite;
		
		/**
		 * Constructor
		 * @param	x
		 * @param	y
		 */
		public function GCBuyPopup(x:int = 0, y:int = 0) 
		{
			this.x = x;
			this.y = y;
			
			_resourceName = "";
			_pricePerUnit = 0;
			_quantityBought = 0;
			_totalPrice = 0;
			_amountForSale = 0;
			
			_quantityComponent = new GCNumberScroller();
			_confirmButton = new GCButton(onConfirmBuy);
			_cancelButton = new GCButton(onCancelBuy);
			_buyAllButton = new GCButton(onBuyAll);
			
			_resourceLabel = new GCTextField();
			_quantityLabel = new GCTextField();
			_pricePerLabel = new GCTextField();
			_totalPriceLabel = new GCTextField();
			
			_resourceText = new GCTextField();
			_pricePerText = new GCTextField();
			_totalPriceText = new GCTextField();
			_amountForSaleText = new GCTextField();
			
			this.init();
		}
		
		private function init():void
		{
			_quantityComponent.addEventListener(GCEvent.VALUE_CHANGED_EVENT, onValueChanged);
			
			_resourceLabel.text = RESOURCE_LABEL;
			_quantityLabel.text = QUANTITY_LABEL;
			_pricePerLabel.text = PRICE_PER_LABEL;
			_totalPriceLabel.text = TOTAL_PRICE_LABEL;
			
			_resourceLabel.height = LABEL_HEIGHT;
			_resourceLabel.width = LABEL_WIDTH;
			_resourceLabel.x = X_OFFSET;
			_resourceLabel.y = Y_OFFSET;
			
			_quantityLabel.height = LABEL_HEIGHT;
			_quantityLabel.width = LABEL_WIDTH;
			_quantityLabel.y = _resourceLabel.y + _resourceLabel.height;
			_quantityLabel.x = X_OFFSET;
			
			_pricePerLabel.height = LABEL_HEIGHT;
			_pricePerLabel.width = LABEL_WIDTH;
			_pricePerLabel.y = _quantityLabel.y + _quantityLabel.height + 5;
			_pricePerLabel.x = X_OFFSET;
			
			_totalPriceLabel.height = LABEL_HEIGHT;
			_totalPriceLabel.y = _pricePerLabel.y + _pricePerLabel.height;
			_totalPriceLabel.width = LABEL_WIDTH;
			_totalPriceLabel.x = X_OFFSET;
			
			_resourceText.text = _resourceName;
			_resourceText.width = LABEL_WIDTH;
			_resourceText.height = LABEL_HEIGHT;
			_resourceText.x = _resourceLabel.x + _resourceLabel.width;
			_resourceText.y = _resourceLabel.y;
			
			_quantityComponent.x = _quantityLabel.x + _quantityLabel.width;
			_quantityComponent.y = _quantityLabel.y;
			_amountForSaleText.x = _quantityComponent.x + _quantityComponent.width;
			_amountForSaleText.y = _quantityComponent.y;
			
			_pricePerText.text = String(_pricePerUnit);
			_pricePerText.width = 50;
			_pricePerText.height = LABEL_HEIGHT;
			_pricePerText.x = _pricePerLabel.x + _pricePerLabel.width;
			_pricePerText.y = _pricePerLabel.y;
			
			_totalPriceText.text = String(_totalPrice);
			_totalPriceText.width = 50;
			_totalPriceText.height = LABEL_HEIGHT;
			_totalPriceText.x = _totalPriceLabel.x + _totalPriceLabel.width;
			_totalPriceText.y = _totalPriceLabel.y;
			
			/*
			The buttons 
			*/
			_confirmButton.loadGraphic(GCResources.plate, 100, 30);
			_confirmButton.loadText("OK");
			_confirmButton.x = 10;
			_confirmButton.y = _totalPriceLabel.y + LABEL_HEIGHT;
			
			_cancelButton.loadGraphic(GCResources.plate, 100, 30);
			_cancelButton.loadText("Cancel");
			_cancelButton.x = _confirmButton.x + _confirmButton.width;
			_cancelButton.y = _confirmButton.y;			
			
			_buyAllButton.loadGraphic(GCResources.plate, 100, 30);
			_buyAllButton.loadText("Buy All");
			_buyAllButton.y = _amountForSaleText.y + _amountForSaleText.height / 2;
			_buyAllButton.x = _amountForSaleText.x;
			
			this.addChild(_resourceLabel);
			this.addChild(_quantityLabel);
			this.addChild(_pricePerLabel);
			this.addChild(_totalPriceLabel);
			
			this.addChild(_resourceText);
			this.addChild(_pricePerText);
			this.addChild(_totalPriceText);
			this.addChild(_amountForSaleText);
			
			this.addChild(_quantityComponent);
			this.addChild(_confirmButton);
			this.addChild(_cancelButton);
			this.addChild(_buyAllButton);
			
			//Popup background
			_backgroundImage = GCResources.getMaskedCanvasBackground(this.width, this.height);
			this.addChildAt(_backgroundImage, 0);
		}		
		
		//----------------------------------------------------------------------
		//	PUBLIC FUNCTIONS
		//----------------------------------------------------------------------		
		
		/**
		 * Function to initialize the buy popup. Should be called right
		 * before it is added to the display.
		 */
		public function initializePopup(quantity:int, pricePer:int, resourceName:String):void
		{
			_amountForSale = quantity;
			_amountForSaleText.text = "Max can buy: " + String(_amountForSale);
			_quantityComponent.setLimit(_amountForSale);
			_pricePerUnit = pricePer;
			_resourceName = resourceName;
			
			_resourceText.text = _resourceName;
			_quantityComponent.setValue(0);
			_pricePerText.text = String(_pricePerUnit);
			
			// Enable the buttons
			_confirmButton.enable();
			_cancelButton.enable();
			_buyAllButton.enable();			
		}
		
		/**
		 * Function to clean up the popup data. Should be called
		 * right before removing it from the display.
		 */
		public function clearPopup():void
		{
			_quantityComponent.setLimit(GCNumberScroller.MAX_VALUE);
			_quantityComponent.setValue(0);
			_pricePerUnit = 0;
			_resourceName = "";
			_quantityBought = 0;
			_totalPrice = 0;
		}
		
		
		//----------------------------------------------------------------------
		//	PRIVATE FUNCTIONS
		//----------------------------------------------------------------------
		
		/**
		 *	Handles clean up of the pop up
		 */
		private function onExitCleanup():void
		{
			// Disable the buttons
			_confirmButton.disable();
			_cancelButton.disable();
			_buyAllButton.disable();
		}		
		
		//----------------------------------------------------------------------
		//	EVENT HANDLERS
		//----------------------------------------------------------------------
		
		/**
		 * When the quantity component has one of its value changed,
		 * we must update the total price that the user must pay.
		 * 
		 * @param	event
		 */
		private function onValueChanged(event:Event):void
		{
			_quantityBought = _quantityComponent.value;
			_totalPrice = _quantityBought * _pricePerUnit;
			
			_totalPriceText.text = String(_totalPrice);
		}
		
		/**
		 * Handles the buying confirmation event
		 * @param	event
		 */
		private function onConfirmBuy(event:MouseEvent):void
		{
			// Clean up the popup before closing
			onExitCleanup();
			
			if (_quantityBought > 0)
			{
				dispatchEvent(new Event(GCEvent.BUY_CONFIRM_EVENT));
			}
			else
			{
				dispatchEvent(new Event(GCEvent.CANCEL_EVENT));
			}			
		}
		
		/**
		 * Handles buy canceling event
		 * @param	event
		 */
		private function onCancelBuy(event:MouseEvent):void
		{
			// Cleans up the pop up before closing
			onExitCleanup();
			
			dispatchEvent(new Event(GCEvent.CANCEL_EVENT));			
		}
		
		/**
		 * Sets the scroller count to the max value
		 * @param	event
		 */
		private function onBuyAll(event:MouseEvent):void
		{
			_quantityComponent.setValue(_amountForSale);
		}		
		
		//----------------------------------------------------------------------
		//	GET FUNCTIONS
		//----------------------------------------------------------------------		
		
		/**
		 * Get the amount of goods that were purchased.
		 */
		public function get quantityBought():int
		{
			return _quantityBought;
		}
		
		/**
		 * Get the total price paid for the goods.
		 */
		public function get totalCost():int
		{
			return _totalPrice;
		}
		
		
	}
}